<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.sql.*" import="Connect.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1></h1>
		<hr></hr>
		<%
			// Get form information.
			String auction_id = request.getParameter("id");
			String new_bid = request.getParameter("bid" + auction_id);

			// Get session information.
			String user = (String) session.getAttribute("logged_user");

			//Delete the user from all tables.
			java.sql.Connection conn = null;
			try {
				// Connect to the database.
				Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
				java.util.Properties sysprops = System.getProperties();
				sysprops.put("user", ConnectionInfo.myUserID());
				sysprops.put("password", ConnectionInfo.myPassword());
				conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);

				// Update the information for the specified user.
				java.sql.Statement statement = conn.createStatement();
				java.sql.ResultSet rs = statement.executeQuery("SELECT * from auctions RIGHT JOIN bids ON id=auction_id WHERE auction_id = '"
						+ auction_id + "' ORDER BY bid_price DESC, bid_time ASC");
				String errors = "";
				double bid = 0.0;
				try {
					// Check if the bid is a double.
					bid = Double.valueOf(new_bid);
					if (rs.next()) {
						double minimum_bid = Double.parseDouble(rs.getString("minimum_bid"));
						double bid_increment = Double.parseDouble(rs.getString("bid_increment"));
						double highest_bid = Double.parseDouble(rs.getString("bid_price"));
						String highest_bidder = rs.getString("bidder_username");
						String auction_owner = rs.getString("owner");
						// Determine the current bid.
						if (rs.next() == false) {
							highest_bid = minimum_bid;
						} else {
							double next_highest_bid = Double.parseDouble(rs.getString("bid_price"));
							highest_bid = next_highest_bid + bid_increment;
						}
						// Check bid conditions.
						if (bid < minimum_bid) {
							errors += "* Your bid is lower than the minimum id.<br/>";
						}
						if (bid <= highest_bid) {
							errors += "* Your bid is lower than or equal to the current bid. <br/>";
						}
						if (ConnectionInfo.twoSigFigs((bid - minimum_bid) / bid_increment) % 1 != 0.0) {
							errors += "* Your bid must be a multiple of the bid_increment plus the minimum_bid.<br />";
						}
						if (highest_bidder.equalsIgnoreCase(user)) {
							errors += "* You are already the high bidder of this auction. <br/>";
						}
						if(((String)session.getAttribute("logged_user")).equalsIgnoreCase(auction_owner)){
							errors += "* You can't bid on your own auctions. <br/>";
						}
					} else {
						// There is no bids so far so only check minimum bid.
						rs = statement.executeQuery("SELECT * from auctions WHERE id = '" + auction_id + "'");
						
						if (rs.next()) {
							double minimum_bid = Double.parseDouble(rs.getString("minimum_bid"));
							if (bid < minimum_bid) {
								errors += "* Your bid is lower than the minimum bid.<br/>";
							}
							if(((String)session.getAttribute("logged_user")).equalsIgnoreCase(rs.getString("owner"))){
								errors += "* You can't bid on your own auctions. <br/>";
							}
						}
						
					}
				} catch (Exception e) {
					out.println(e.getMessage());
					errors += "* You did not enter a number as a bid.<br/>";
				}
				if (!errors.equalsIgnoreCase("")) {
					// Print errors.
					out.println("<div class=\"section padding-5px error-text\">");
					out.println(errors);
					out.println("</div>");
				} else {
					// No errors insert the new bid.
					String values = "'" + auction_id + "','" + user + "'," + "NOW(),'" + bid + "'";
					statement.executeUpdate("INSERT INTO bids(auction_id, bidder_username, bid_time, bid_price) VALUES(" + values + ")");
					out.println("<b>Bid successfully placed!</b><br />");
					out.println("<a href=\"view_auctions.jsp#" + auction_id + "\">Click here if you can't wait!</a>");
				}

				session.setAttribute("item_id", request.getParameter("item_id"));
				out.println("You are being redirected in 5 seconds.");
				response.setHeader("Refresh", "5; view_auctions.jsp#" + auction_id);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					conn.close();
				} catch (Exception ee) {
				}
			}
		%>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>